<?php
class MFile {
    
    public static function createDirectoryRecursively($path){
        $parts = preg_split('#/|'.preg_quote(DS).'#', $path, -1, PREG_SPLIT_NO_EMPTY);
        $basePath = '';
        foreach ($parts as $dir) {
            if (!file_exists($basePath.$dir)){
                mkdir($basePath.$dir);
            }
            $basePath .= $dir.DS;
        }
    }
    
    /**
     * Copy the content of the source directory to the destination directory recursively
     * @param String $src : The absolute path of the source directory
     * @param String $dest : The absolute path of the destination directory
     * @return Boolean : Copy status
     */
    public static function copyDirectory($src, $dest){
        if (is_file($src)){
            if(@filemtime($dest)<@filemtime($src)){
                return copy($src, $dest);
            } else
                return true;
        }

        if (!is_dir($dest)){
            @mkdir($dest);
            @chmod($dest, 0777);
        }

        $dir = dir($src);
        while (false !== $entry = $dir->read()){
            if ($entry == '.' || $entry == '..')
                continue;

            self::copyDirectory($src.'/'.$entry, $dest.'/'.$entry);
        }

        $dir->close();
        return true;
    }
    
    /**
     * Read the content of a file an return an array of SQL queries in this
     * @param String $pathFile
     * @return Array 
     */
    public static function getSQLQueries($pathFile){
        $requetes = '';
        $sql = file($pathFile);
        foreach ($sql as $l){
            if (substr(trim($l),0,2)!="--"){
                $requetes .= $l;
            }
        }
        return explode(';', trim($requetes));
    }
    
    public static function removeDirectoryRecursively($dir, $skipFiles=array('.','..')) {
        if (file_exists($path)){
            $dir .= (substr($dir,0,-1) == '/') ? '' : '/'; 
            foreach (scandir($dir) as $file) 
            { 
                if(in_array($file,$skipFiles) === false) 
                { 
                    if(is_dir($dir.$file)) rmdir_rec($dir.$file); 
                    (is_dir($dir.$file)) ? rmdir($dir.$file) : unlink($dir.$file); 
                } 
            }
        }
    }
    
    public static function getPHPFileContent($file){
        if (is_file($file)){
            ob_start();
            require $file;
            $content = ob_get_contents();
            ob_end_clean();
            return $content;
        }
        return false;
    }

    public static function getExtension($path){
        $pathInfo = pathinfo($path);
        return $pathInfo['extension'];
    }
    
}
?>
